package com.yanqu.road.server.cmd.assistant;

import com.yanqu.road.entity.union.parergon.UnionParergon;
import com.yanqu.road.entity.union.parergon.UnionParergonOperateResult;
import com.yanqu.road.pb.assistant.AssistantProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.CrossUnionMgr;
import com.yanqu.road.server.manager.union.CrossUnionParergonMgr;
import com.yanqu.road.server.manager.union.CrossUnionReqContext;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.C_UNION_ASSISTANT_ACTION, desc = "处理跨服商会小助理操作")
public class CrossAssistantUnionCrossActionCmd extends CrossCommand {

    private AssistantProto.AssistantCrossActionReqMsg crossMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        crossMsg = AssistantProto.AssistantCrossActionReqMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        AssistantProto.AssistantActionReqMsg.Builder assistantActionReqMsg = crossMsg.getAssistantActionReqMsg().toBuilder();
        AssistantProto.AssistantActionRespMsg.Builder assistantActionRespMsg = crossMsg.getAssistantActionRespMsg().toBuilder();

        AssistantProto.AssistantCrossActionRespMsg.Builder builder = AssistantProto.AssistantCrossActionRespMsg.newBuilder();
        builder.setAssistantActionReqMsg(assistantActionReqMsg);
        builder.setAssistantActionRespMsg(assistantActionRespMsg);

        for (AssistantProto.AssistantUnionCrossBuildTemp buildTemp : crossMsg.getBuildList()) {
            builder.addResult(CrossUnionMgr.assistantConstructUnion(packet.getPlayerId(), buildTemp.getConstructionType(), buildTemp.hasConsume() ? buildTemp.getConsume(): ""));
        }

        if(crossMsg.hasOneKeyReceiveActive()){
            builder.setOneKeyReceive(CrossUnionMgr.oneKeyReceiveUnionConstructActiveReward(serverId, packet.getPlayerId()));
        }

        if(crossMsg.hasOneKeyOperate()){
            UnionParergonOperateResult result = new UnionParergonOperateResult();
            CrossUnionReqContext context = CrossUnionReqContext.initContext(serverId, packet.getPlayerId()).loadUnionMember().loadUnionInfo();
            if(context.getRet() == 0){
                UnionParergon parergon = CrossUnionParergonMgr.getUnionParergon(context.getUnionInfo().getUnionUid());
                if(parergon == null){
                    result.setRet(GameErrorCode.E_PARERGON_NOT_IN_UNION);
                }else {
                    result = CrossUnionMgr.assistantParergonOperate(serverId, packet.getPlayerId(), context.getUnionInfo().getUnionUid());
                }
            }else {
                result.setRet(context.getRet());
            }
            builder.setOperate(CrossUnionParergonMgr.builderOperateRespMsg(result));
        }

        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.S_CROSS_ASSISTANT_ACTION_FOR_UNION_FROM_CROSS, builder));

    }

    @Override
    public void sendData() {

    }
}
